﻿using CommonNet48.LogUtils.Log4net;
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.POCO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevUIUtils.MVVM.BaseViewModels
{
    [POCOViewModel]
    public abstract class BaseViewModel
    {
        public BaseViewModel()
        {
            _Logger = new Log4(this.GetType());
        }
        /// <summary>
        /// 日志操作对象
        /// </summary>
        protected Log4 _Logger;

        /// <summary>
        /// 对话框服务
        /// </summary>
        protected virtual IMessageBoxService MessageBoxService => this.GetService<IMessageBoxService>();

        /// <summary>
        /// 跨线程操作服务
        /// </summary>
        protected virtual IDispatcherService DispatcherService => this.GetService<IDispatcherService>();

        /// <summary>
        /// 屏幕等待服务
        /// </summary>
        protected ISplashScreenService SplashScreenService => this.GetService<ISplashScreenService>();

        /// <summary>
        /// 文档操作服务
        /// </summary>
        protected IDocumentManagerService DocumentManagerService => this.GetService<IDocumentManagerService>();

        /// <summary>
        /// 文件打开对话框服务(单选)。
        /// 使用前先要在View层用mvvmContext.RegCustomSingleOpenFileDialog()进行注册
        /// </summary>
        protected IOpenFileDialogService SingleOpenFileDialogService => this.GetService<IOpenFileDialogService>("SingleOpenFileDialog");

        /// <summary>
        /// 文件打开对话框服务(多选)
        /// </summary>
        protected IOpenFileDialogService MultipleOpenFileDialogService => this.GetService<IOpenFileDialogService>("MultiselectOpenFileDialog");

        /// <summary>
        /// 获取文件打开对话框服务(根据注册Key获取）
        /// </summary>
        /// <param name="key">注册时的key关键字</param>
        /// <returns></returns>
        protected IOpenFileDialogService GetOpenFileDialogService(string key)
        {
            return this.GetService<IOpenFileDialogService>(key);
        }

        /// <summary>
        /// 文件保存对话框服务
        /// </summary>
        protected ISaveFileDialogService SaveFileDialogService => this.GetService<ISaveFileDialogService>();


        /// <summary>
        /// 获取普通窗体服务对象(需要引入PresentationFramework程序集)
        /// </summary>
        /// <param name="title">窗体标题</param>
        /// <returns></returns>
        protected virtual IWindowService GetGeneralFormService(string title)
        {
            try
            {
                IWindowService ser = this.GetService<IWindowService>("Window");
                if (ser == null) return null;
                ser.Title = title;
                return ser;
            }
            catch (Exception ex)
            {
                string err = $"获取{title}窗体对象失败！";
                _Logger.Error(err, ex);
                return null;
            }

        }


        /// <summary>
        /// 获取对话框窗体服务对象(需要引入PresentationFramework程序集)
        /// </summary>
        /// <param name="title">窗体标题</param>
        /// <param name="key">对话框Key</param>
        /// <returns></returns>
        protected virtual IWindowService CreateDialogFormService(string title, string key = "Dialog")
        {
            try
            {
                IWindowService ser = this.GetService<IWindowService>(key);
                if (ser == null) return null;
                ser.Title = title;
                return ser;
            }
            catch (Exception ex)
            {
                string err = $"获取{title}对话框窗体失败！";
                _Logger.Error(err, ex);
                return null;
            }

        }

        /// <summary>
        /// 展示错误信息
        /// </summary>
        /// <param name="msg">信息内容</param>
        protected virtual void ShowErrMSG(string msg)
        {
            MessageBoxService.ShowMessage(msg, "错误", MessageButton.OK, MessageIcon.Error);
        }

        /// <summary>
        /// 展示提示信息
        /// </summary>
        /// <param name="msg">提示内容</param>
        protected virtual void ShowInfoMSG(string msg)
        {
            MessageBoxService.ShowMessage(msg, "提示", MessageButton.OK, MessageIcon.Information);
        }

        /// <summary>
        /// 展示询问信息并返回结果
        /// </summary>
        /// <param name="msg">询问信息</param>
        /// <param name="msgBtn">询问按钮类型(MessageButton枚举类型)</param>
        protected virtual MessageResult ShowQuestionMSG(string msg, MessageButton msgBtn = MessageButton.YesNo)
        {
            return MessageBoxService.ShowMessage(msg, "询问", msgBtn, MessageIcon.Question);
        }
    }
}
